<template>
  <div id="List_container">
    <!-- 卡片区 -->
    <el-card>
      <!-- 搜索与添加区 -->
      <el-row :gutter="20">
        <el-col :span="18">
          <el-input
            v-model.trim="queryInfo.query"
            @keyup.enter.native="queryInfo.query && getGoodsList()"
            placeholder="请输入内容"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              @click="queryInfo.query && getGoodsList()"
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddpage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- table 表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index" align="center"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" show-overflow-tooltip></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="110px" align="center"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80px" align="center"></el-table-column>
        <el-table-column label="创建时间" width="100px" align="center">
          <template slot-scope="scope">{{scope.row.add_time | DateFormat}}</template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column label="操作" width="130" header-align="center">
          <template slot-scope="scope">
            <el-button
              type="success"
              icon="el-icon-edit"
              plain
              round
              size="mini"
              @click="showEidtDialog(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              plain
              round
              size="mini"
              @click="removeCateById(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, next, jumper"
        :total="total"
        background
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询条件
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 商品列表数据
      goodsList: [],
      total: 0,
      // 控制 添加商品对话框 的显示与隐藏
      addCateDialogVisible: false
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 根据分页获取相应的商品列表
    async getGoodsList() {
      const { data } = await this.$axios.get('goods', {
        params: this.queryInfo
      })
      if (data.meta.status !== 200) {
        return this.$message.error('获取商品列表数据失败！')
      }
      // console.log(data.data)
      // 赋值
      this.goodsList = data.data.goods
      // 总页数条数
      this.total = data.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getGoodsList()
    },
    // 监听 pagesize 最新的页码
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getGoodsList()
    },
    // 点击按钮展示点击商品的对话框
    showAddCateDialog() {
      this.addCateDialogVisible = true
    },
    // 根据 id 删除对应的商品信息
    async removeCateById(id) {
      const confirmResult = await this.$confirm(
        '此操作将永久删除该商品, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除！')
      }

      const { data } = await this.$axios.delete(`goods/${id}`)

      if (data.meta.status !== 200) {
        return this.$message.error('删除该商品失败！')
      }
      this.$message.success({ message: '删除该商品成功', duration: 1000 })
      this.getGoodsList()
    },
    goAddpage() {
      this.$router.push('/goods/add')
    },
    showEidtDialog(goods_id) {
      // console.log(goods_id)
      this.$router.push({
        path: '/goods/edit',
        query: {
          goods_id: goods_id,
        }
      })
      // console.log('编辑操作')
    }
  }
}
</script>

<style lang="css" scoped>
</style>